/*
 * Copyright (C) 2016 yixun All Rights Reserved.
 * 
 * AreaServiceImpl.java
 */
package com.emm.yixun.basic.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.mapper.SalesGroupUserMapper;
import com.emm.yixun.basic.service.AppPushService;
import com.emm.yixun.basic.service.UserTokenService;
import com.emm.yixun.common.model.SalesGroupUser;
import com.emm.yixun.common.model.UserToken;
import com.emm.yixun.common.model.dto.UserTokenDto;
import com.emm.yixun.common.model.enums.StatusEnum;
import com.emm.yixun.common.utils.AppPushUtils;

/**
 * [文件名称]<br>
 * AppPushServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 地区表 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2016-05-11 16:47:20 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("appPushService")
public class AppPushServiceImpl implements AppPushService {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private UserTokenService userTokenService;

	@Autowired
	private SalesGroupUserMapper salesGroupUserMapper;

	public void pushMessage(String userId, String alert, String title, Map<String, String> extraMap) {
		AppPushUtils appPushUtils = new AppPushUtils();
		UserTokenDto userTokenDto = new UserTokenDto();
		try {
			userTokenDto.setUserId(Long.valueOf(userId));
			userTokenDto.setStatus(StatusEnum.LOGINED.getNo());
			UserToken userToken = userTokenService.selectByDto(userTokenDto);
			if (null != userToken) {
				if (userToken.getDeviceToken() != null && !"".equals(userToken.getDeviceToken())) {
					appPushUtils.pushMessage(userId, alert, title, extraMap, userToken.getDeviceType(),
							userToken.getDeviceToken());
				}
				// }else{
				// throw new BusinessException("9999","没有找到会员对应的设备");
			}
		} catch (Exception e) {
			logger.error("pushMessage error: ", e);
		}
	}

	public void pushMessage2(Long merchantId, String productCode, String alert, String title,
			Map<String, String> extraMap) {
		AppPushUtils appPushUtils = new AppPushUtils();
		UserTokenDto userTokenDto = new UserTokenDto();
		try {
			List<SalesGroupUser> selectUserIdList = salesGroupUserMapper.selectUserIdList(merchantId, productCode);
			if (selectUserIdList.size() > 0) {
				for (SalesGroupUser salesGroupUser : selectUserIdList) {
					userTokenDto.setUserId(salesGroupUser.getUserId());
					userTokenDto.setStatus(StatusEnum.LOGINED.getNo());
					UserToken userToken = userTokenService.selectByDto(userTokenDto);
					if (null != userToken) {
						if (userToken.getDeviceToken() != null && !"".equals(userToken.getDeviceToken())) {
							appPushUtils.pushMessage(String.valueOf(userToken.getUserId()), alert, title, extraMap,
									userToken.getDeviceType(), userToken.getDeviceToken());
						}
					}
					// else{
					// throw new BusinessException("9999","没有找到会员对应的设备");
					// }
				}
			}
		} catch (Exception e) {
			logger.error("pushMessage2 error", e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.emm.yixun.basic.service.AppPushService#jobPushMessage(java.lang.String, java.lang.String,
	 * java.lang.String, java.util.Map)
	 */
	public String jobPushMessage(String userId, String alert, String title, Map<String, String> extraMap) {
		String flag = "0";
		AppPushUtils appPushUtils = new AppPushUtils();
		try {
			UserToken userToken = userTokenService.selectByUserId(Long.valueOf(userId));
			if (null != userToken && StringUtils.isNotBlank(userToken.getDeviceToken())) {
				appPushUtils.pushMessage(userId, alert, title, extraMap, userToken.getDeviceType(),
						userToken.getDeviceToken());
				flag = "1";
			}

		} catch (Exception e) {
			logger.error("jobPushMessage error", e);
			flag = "2";
		}
		return flag;
	}
}
